//
//  SupplenessTestingViewController.m
//  安好
//
//  Created by qinlongqing on 14/12/3.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "SupplenessTestingViewController.h"
#import "ClickEffectLayer.h"
#import "SupplenessViewController.h"
#import "AppConfig.h"

CGFloat maxMovedDistance = 8.0;

@interface SupplenessTestingViewController ()
{
    ClickEffectLayer *waveLayer1;
    ClickEffectLayer *waveLayer2;
    
    CGPoint firstTouchedPoint1;
    CGPoint firstTouchedPoint2;
    
    NSTimer *keepTimer;
    CGFloat keepedTime;
    
    BOOL isFinish;
}
@end

@implementation SupplenessTestingViewController

- (void)viewWillAppear:(BOOL)animated{
    isFinish = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = UIColorFromRGB(0x4fc1e9);
    
    [self makeNavBar];

    UILabel *tiShiLabel = [[UILabel alloc]init];
    if (kScreenWidth == 414) {
        tiShiLabel.frame = CGRectMake(20, kScreenHeight - 64 - 30 - 70, kScreenWidth - 40, 30);
        tiShiLabel.font = [UIFont systemFontOfSize:17];
    }
    else{
        tiShiLabel.frame = CGRectMake(15, kScreenHeight - 64 - 30 - 50, kScreenWidth - 30, 30);
        tiShiLabel.font = [UIFont systemFontOfSize:15];
    }
    tiShiLabel.textAlignment = NSTextAlignmentCenter;
    tiShiLabel.textColor = [UIColor whiteColor];
    tiShiLabel.text = @"双手食指触摸屏幕,并尽力保持";
    tiShiLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tiShiLabel];
    
    
    isFinish = NO;
    
    waveLayer1 = [ClickEffectLayer layer];
    waveLayer2 = [ClickEffectLayer layer];
    
    self.view.multipleTouchEnabled = YES;
}

-(void)makeNavBar{
    self.navigationItem.title = @"柔韧性";
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnclicked)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}

-(void)backBtnclicked{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [waveLayer1 removeFromSuperlayer];
    [waveLayer2 removeFromSuperlayer];
    
    if (isFinish == NO) {
        NSArray *allTouchsArray = event.allTouches.allObjects;//得到所有的点击
        if (allTouchsArray.count > 1) {
            keepTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(doCountTimer) userInfo:nil repeats:YES];
            keepedTime = 0.0;
            
            CGPoint touchPoint1 = [[allTouchsArray objectAtIndex:0] locationInView:self.view];
            waveLayer1.currentPoint = touchPoint1;
            firstTouchedPoint1 = touchPoint1;
            [self.view.layer addSublayer:waveLayer1];
            
            
            CGPoint touchPoint2 = [[allTouchsArray objectAtIndex:1] locationInView:self.view];
            waveLayer2.currentPoint = touchPoint2;
            firstTouchedPoint2 = touchPoint2;
            [self.view.layer addSublayer:waveLayer2];
        }
        else{
            
        }
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    if (isFinish == NO) {
        NSArray *allTouchsArray = event.allTouches.allObjects;//得到所有的点击
        if (allTouchsArray.count > 1) {
            CGPoint touchPoint1 = [[allTouchsArray objectAtIndex:0] locationInView:self.view];
            waveLayer1.currentPoint = touchPoint1;
            
            CGPoint touchPoint2 = [[allTouchsArray objectAtIndex:1] locationInView:self.view];
            waveLayer2.currentPoint = touchPoint2;
            
            CGFloat movedDistance1 = sqrtf(powf((firstTouchedPoint1.x - touchPoint1.x), 2) + powf((firstTouchedPoint1.y - touchPoint1.y), 2));
            CGFloat movedDistance2 = sqrtf(powf((firstTouchedPoint2.x - touchPoint2.x), 2) + powf((firstTouchedPoint2.y - touchPoint2.y), 2));
            if (movedDistance1 > maxMovedDistance || movedDistance2 > maxMovedDistance) {
                isFinish = YES;
                
                [waveLayer1 removeFromSuperlayer];
                [waveLayer2 removeFromSuperlayer];
                
                [keepTimer invalidate];
                keepTimer = nil;
                
                [self.supplenessViewController loadTestResult:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:keepedTime],kRouRenXingTestKeepedTime, nil]];
            }
        }
        else{
            
        }
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [waveLayer1 removeFromSuperlayer];
    [waveLayer2 removeFromSuperlayer];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    [waveLayer1 removeFromSuperlayer];
    [waveLayer2 removeFromSuperlayer];
}

- (void)doCountTimer{
    keepedTime += 0.1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
